package com.sgcc.fj.pmos.e_fj_mall.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.fj.pmos.arouter.mall.MallArouterPath;
import com.sgcc.fj.pmos.common.base.MvpBaseActivity;
import com.sgcc.fj.pmos.common.bean.EventCenter;
import com.sgcc.fj.pmos.common.bean.PowDetileByChartBean;
import com.sgcc.fj.pmos.common.bean.PowMonth;
import com.sgcc.fj.pmos.common.bean.PowMonthCoye;
import com.sgcc.fj.pmos.common.bean.RatioCurveBean;
import com.sgcc.ln.pmos.common.widget.TitleBar;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.e_fj_mall.adapter.PowChartAdapter;
import com.sgcc.fj.pmos.e_fj_mall.adapter.PowersetChartAdapter;
import com.sgcc.fj.pmos.e_fj_mall.contract.PowSetByChartContract;
import com.sgcc.fj.pmos.e_fj_mall.fragment.PowerSetFragment;
import com.sgcc.fj.pmos.e_fj_mall.presenter.PowSetByChartPresenter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * 套餐详情
 * <p>
 * android锚点定位 Android 实现锚点定位思路详解
 * http://www.qb5200.com/article/353138.html
 */
@Route(path = MallArouterPath.MALL_POWSETCHART)
public class PowSetByChartActivity extends MvpBaseActivity<PowSetByChartPresenter> implements PowSetByChartContract.View {

    private TitleBar mTitleBar;
    private PowersetChartAdapter mPowersetAdapter;
    private PowChartAdapter powChartAdapter;
    //    powChartAdapter
    private RecyclerView mRecycleview;
    private RecyclerView recycleview_chart;
    private TextView next2;
    public List<PowMonthCoye> mData = new ArrayList<>();
    public ArrayList<RatioCurveBean.Bean> mDataChart = new ArrayList<>();
    private TextView save_txt;
    private EditText edit_total;
    private Map<Integer, String> editMap = new HashMap<>();
    private Map<Integer, Integer> chartTypeMap = new HashMap<>();
    private int monthPosition = 0;
    private int lastPosition;
    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_powselect_bychart;
    }

    @Override
    protected PowSetByChartPresenter createPresenter() {
        return new PowSetByChartPresenter();
    }


    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Override
    public void onEventCallBack(EventCenter eventCenter) {
        super.onEventCallBack(eventCenter);
    }

    @Override
    protected void initViews() {
        super.initViews();
        save_txt = findViewById(R.id.save_txt);
        save_txt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(!isMapHaveNull(editMap,chartTypeMap,mData.size())){
                    Intent intent = new Intent();
                    PowDetileByChartBean bean = new PowDetileByChartBean();
                    bean.setList(init24Data());
                    intent.putExtra("monthList", bean);
                    setResult(PowerSetFragment.CHARTSELECT_SUCCSS, intent);
                    finish();
                }else{
                    ToastUtils.showShort("请给每个月设置总量，并选择一条曲线");
                }


            }
        });

        edit_total = findViewById(R.id.edit_total);
        edit_total.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                editMap.put(monthPosition, edit_total.getText().toString());
//                if(TextUtils.isEmpty(charSequence)){
//                    txt_total.setVisibility(View.GONE);
//                }else{
//                    txt_total.setVisibility(View.VISIBLE);
//                }
            }

            @Override
            public void afterTextChanged(Editable editable) {


            }
        });
        mRecycleview = findViewById(R.id.recycleview);
        recycleview_chart = findViewById(R.id.recycleview_chart);
        mTitleBar = (TitleBar) findViewById(R.id.title_bar);
        mTitleBar.setTitle("议价电量");
        mTitleBar.setRightIcon(R.mipmap.go_main, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                goMainActivity();
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        LinearLayoutManager layoutManagerChart = new LinearLayoutManager(this);
        layoutManagerChart.setOrientation(LinearLayoutManager.VERTICAL);
        mRecycleview.setLayoutManager(layoutManager);
        recycleview_chart.setLayoutManager(layoutManagerChart);


        mPowersetAdapter = new PowersetChartAdapter();
        mPowersetAdapter.setItemClickListener(new PowersetChartAdapter.OnItemClickListener() {
            @Override
            public void OnItemsClickListener(int position, PowMonth s, int lastpositon) {
                monthPosition = position;
                if (!TextUtils.isEmpty(editMap.get(position))) {
                    edit_total.setText(editMap.get(position));
                } else {
                    edit_total.setText("");
                }
                mData.get(lastpositon).setSelect(false);
                mData.get(position).setSelect(true);
                mPowersetAdapter.notifyDataSetChanged();

                mDataChart.get(lastPosition).setSelect(false);
                powChartAdapter.notifyDataSetChanged();

            }
        });
        powChartAdapter = new PowChartAdapter(this);
        Intent inten = getIntent();
        initMonthList(inten.getIntExtra("startMonth", 0), inten.getIntExtra("endMonth", 0));
        mPowersetAdapter.setNewData(mData);
        mPowersetAdapter.notifyDataSetChanged();
        mPowersetAdapter.setYear(inten.getStringExtra("year"));
        mRecycleview.setAdapter(mPowersetAdapter);
        mPresenter.getChartList();

        powChartAdapter.setItemClickListener(new PowChartAdapter.OnItemClickListener() {
            @Override
            public void OnItemsClickListener(int position, RatioCurveBean.Bean s,int lastPosition) {
                chartTypeMap.put(monthPosition, position);
               PowSetByChartActivity.this. lastPosition=lastPosition;
                mDataChart.get(lastPosition).setSelect(false);
                mDataChart.get(position).setSelect(true);
                powChartAdapter.notifyDataSetChanged();

            }
        });


    }

    @Override
    public void getChartListSuccess(ArrayList<RatioCurveBean.Bean> chartList) {
        mDataChart = chartList;
//        if(mDataChart.size()!=0){
//            mDataChart.get(0).setSelect(true);
//        }
        powChartAdapter.setNewData(mDataChart);
        powChartAdapter.notifyDataSetChanged();
        recycleview_chart.setAdapter(powChartAdapter);
    }

    @Override
    public void getChartListFailed() {

    }


    public void initMonthList(int startMonth, int endMonth) {
//        boolean isPositonOne = true;
        for (int i = startMonth; i < endMonth + 1; i++) {
            PowMonthCoye powMonthCoye = new PowMonthCoye();
            PowMonth powMont = new PowMonth();
            powMont.setMonthDate(i);
//            if (isPositonOne) {
//                isPositonOne = false;
//                powMonthCoye.setSelect(true);
//            } else {
//                powMonthCoye.setSelect(false);
//            }
            powMonthCoye.setPowMonth(powMont);
            mData.add(powMonthCoye);
        }
//        if(mData.size()>0){
//            mData.get(0).setSelect(true);
//        }
    }

    /**
     * 组合数据
     * 根据存储的每个月下所属的总量以及用户选择的曲线，计算出每个月24个节点对应的具体电量数据
     */
    private List<PowMonthCoye> init24Data() {
        if(!isMapHaveNull(editMap,chartTypeMap,mData.size())){
        for (int i = 0; i < mData.size(); i++) {
            int totalNum = Integer.valueOf(editMap.get(i));//得到当前坐标输入的总量
            String yAxi1 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(0);//得到当前坐标下 用户选择的曲线中第一个节点
            String yAxi2 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(1);
            String yAxi3 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(2);
            String yAxi4 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(3);
            String yAxi5 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(4);
            String yAxi6 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(5);
            String yAxi7 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(6);
            String yAxi8 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(7);
            String yAxi9 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(8);
            String yAxi10 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(9);
            String yAxi11 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(10);
            String yAxi12 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(11);
            String yAxi13 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(12);
            String yAxi14 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(13);
            String yAxi15 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(14);
            String yAxi16 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(15);
            String yAxi17 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(16);
            String yAxi18 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(17);
            String yAxi19 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(18);
            String yAxi20 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(19);
            String yAxi21 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(20);
            String yAxi22 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(21);
            String yAxi23 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(22);
            String yAxi24 = mDataChart.get(chartTypeMap.get(i)).getYAxis().get(23);


            mData.get(i).getPowMonth().setREnergy1(setDouleLenth(yAxi1,totalNum));
            mData.get(i).getPowMonth().setREnergy2(setDouleLenth(yAxi2,totalNum));
            mData.get(i).getPowMonth().setREnergy3(setDouleLenth(yAxi3,totalNum));
            mData.get(i).getPowMonth().setREnergy4(setDouleLenth(yAxi4,totalNum));
            mData.get(i).getPowMonth().setREnergy5(setDouleLenth(yAxi5,totalNum));
            mData.get(i).getPowMonth().setREnergy6(setDouleLenth(yAxi6,totalNum));
            mData.get(i).getPowMonth().setREnergy7(setDouleLenth(yAxi7,totalNum));
            mData.get(i).getPowMonth().setREnergy8(setDouleLenth(yAxi8,totalNum));
            mData.get(i).getPowMonth().setREnergy9(setDouleLenth(yAxi9,totalNum));
            mData.get(i).getPowMonth().setREnergy10(setDouleLenth(yAxi10,totalNum));
            mData.get(i).getPowMonth().setREnergy11(setDouleLenth(yAxi11,totalNum));
            mData.get(i).getPowMonth().setREnergy12(setDouleLenth(yAxi12,totalNum));
            mData.get(i).getPowMonth().setREnergy13(setDouleLenth(yAxi13,totalNum));
            mData.get(i).getPowMonth().setREnergy14(setDouleLenth(yAxi14,totalNum));
            mData.get(i).getPowMonth().setREnergy15(setDouleLenth(yAxi15,totalNum));
            mData.get(i).getPowMonth().setREnergy16(setDouleLenth(yAxi16,totalNum));
            mData.get(i).getPowMonth().setREnergy17(setDouleLenth(yAxi17,totalNum));
            mData.get(i).getPowMonth().setREnergy18(setDouleLenth(yAxi18,totalNum));
            mData.get(i).getPowMonth().setREnergy19(setDouleLenth(yAxi19,totalNum));
            mData.get(i).getPowMonth().setREnergy20(setDouleLenth(yAxi20,totalNum));
            mData.get(i).getPowMonth().setREnergy21(setDouleLenth(yAxi21,totalNum));
            mData.get(i).getPowMonth().setREnergy22(setDouleLenth(yAxi22,totalNum));
            mData.get(i).getPowMonth().setREnergy23(setDouleLenth(yAxi23,totalNum));
            mData.get(i).getPowMonth().setREnergy24(setDouleLenth(yAxi24,totalNum));
        }
        }
        return mData;
    }


    private  String setDouleLenth(String yAxi, int totalNum) {
        BigDecimal two = new BigDecimal(Double.valueOf(yAxi) * totalNum);
        double three = two.setScale(5, BigDecimal.ROUND_HALF_UP).doubleValue();
        return three+""  ;
    }

    public  boolean isMapHaveNull(Map hashMap,Map chartTypeMap,int size) {
        Set<Object> objects = hashMap.keySet();
        Set<Object> chartTypeMaps = chartTypeMap.keySet();
        if(size==objects.size()&&size==chartTypeMaps.size()){
            return false;
        }else{
            return true;
        }
    }
}
